//
//  NSArray+YSLog.m
//  YSKit
//
//  Created by shen on 2018/1/12.
//  Copyright © 2018年 YangShen. All rights reserved.
//

// 字典的分类
@implementation NSDictionary (MBLog)

//#ifdef DEBUG
////打印到控制台时会调用该方法
//- (NSString *)descriptionWithLocale:(id)locale {
//    return self.debugDescription;
//}
//
////有些时候不走上面的方法，而是走这个方法
//- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
//    return self.debugDescription;
//}
//
////用po打印调试信息时会调用该方法
//- (NSString *)debugDescription{
//    NSError *error = nil;
//    //字典转成json
//    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
//    //如果报错了就按原先的格式输出
//    if (error) {
//        return [super debugDescription];
//    }
//    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//    return jsonString;
//}
//#endif

//- (NSString *)descriptionWithLocale:(id)locale {
//    NSMutableString *string = [NSMutableString string];
//
//    // 开头有个{
//    [string appendString:@"{\n"];
//
//    // 遍历所有的键值对
//    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
//        [string appendFormat:@"\t%@", key];
//        [string appendString:@" : "];
//        [string appendFormat:@"%@,\n", obj];
//    }];
//
//    // 结尾有个}
//    [string appendString:@"}"];
//
//    // 查找最后一个逗号
//    NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
//    if (range.location != NSNotFound)
//        [string deleteCharactersInRange:range];
//
//    return string;
//}

- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    NSMutableString *mStr = [NSMutableString string];
    NSMutableString *tab = [NSMutableString stringWithString:@""];
    for (int i = 0; i < level; i++) {
        [tab appendString:@"\t"];
    }
    [mStr appendString:@"{\n"];
    NSArray *allKey = self.allKeys;
    for (int i = 0; i < allKey.count; i++) {
        id value = self[allKey[i]];
        NSString *lastSymbol = (allKey.count == i + 1) ? @"":@";";
        if ([value respondsToSelector:@selector(descriptionWithLocale:indent:)]) {
            [mStr appendFormat:@"\t%@%@ = %@%@\n",tab,allKey[i],[value descriptionWithLocale:locale indent:level + 1],lastSymbol];
        } else {
            [mStr appendFormat:@"\t%@%@ = %@%@\n",tab,allKey[i],value,lastSymbol];
        }
    }
    [mStr appendFormat:@"%@}",tab];
    return mStr;
}
@end


// 数组的分类
@implementation NSArray (MBLog)

- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level{
    NSMutableString *mStr = [NSMutableString string];
    NSMutableString *tab = [NSMutableString stringWithString:@""];
    for (int i = 0; i < level; i++) {
        [tab appendString:@"\t"];
    }
    [mStr appendString:@"[\n"];
    for (int i = 0; i < self.count; i++) {
        NSString *lastSymbol = (self.count == i + 1) ? @"":@",";
        id value = self[i];
        if ([value respondsToSelector:@selector(descriptionWithLocale:indent:)]) {
            [mStr appendFormat:@"\t%@%@%@\n",tab,[value descriptionWithLocale:locale indent:level + 1],lastSymbol];
        } else {
            [mStr appendFormat:@"\t%@%@%@\n",tab,value,lastSymbol];
        }
    }
    [mStr appendFormat:@"%@]",tab];
    return mStr;
}

//- (NSString *)descriptionWithLocale:(id)locale {
//    NSMutableString *string = [NSMutableString string];
//
//    // 开头有个[
//    [string appendString:@"[\n"];
//
//    // 遍历所有的元素
//    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//        [string appendFormat:@"\t%@,\n", obj];
//    }];
//
//    // 结尾有个]
//    [string appendString:@"]"];
//
//    // 查找最后一个逗号
//    NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
//    if (range.location != NSNotFound)
//        [string deleteCharactersInRange:range];
//
//    return string;
//}
@end
